﻿using System.Windows;
using System.Windows.Controls;
using JDKD.BMCS.Business;
using JDKD.BMCS.Common;
using NutShell.DesignPattern;

namespace JDKD.BMCS.AppUI
{
        /// <summary>
        /// FrontAeraPage.xaml 的交互逻辑
        /// </summary>
        public partial class AreaPage : Page
        {
                public AreaPage()
                {
                        InitializeComponent();
                }

                #region 字段

                private BusinessCache cache = Singleton<BusinessCache>.Instance;

                #endregion 字段

                #region 属性

                #endregion 属性

                #region 方法

                private void Page_Loaded(object sender, System.Windows.RoutedEventArgs e)
                {
                        for (int i = 1; i != 13; i++)
                        {
                                StepComboBox.Items.Add(i);
                        }
                        cache.CurrentStep = 1;

                        RootGrid.DataContext = cache;
                }

                #endregion 方法

                private void LeftButton_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
                {
                        if (!cache.IsLogined)
                        {
                                MessageBox.Show("请登录后执行此操作");
                                return;
                        }

                        cache.CurrentArea.X -= cache.CurrentStep;
                        if (cache.CurrentArea.X < Area.Margin)
                        {
                                cache.CurrentArea.X = Area.Margin;
                        }
                        cache.Save();
                }

                private void RightButton_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
                {
                        if (!cache.IsLogined)
                        {
                                MessageBox.Show("请登录后执行此操作");
                                return;
                        }

                        cache.CurrentArea.X += cache.CurrentStep;
                        if (cache.CurrentArea.X > Global.ImageCaptureWidth - Area.Margin)
                        {
                                cache.CurrentArea.X = Global.ImageCaptureWidth - Area.Margin;
                        }
                        cache.Save();
                }

                private void UpButton_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
                {
                        if (!cache.IsLogined)
                        {
                                MessageBox.Show("请登录后执行此操作");
                                return;
                        }
                        cache.CurrentArea.Y -= cache.CurrentStep;
                        if (cache.CurrentArea.Y < Area.Margin)
                        {
                                cache.CurrentArea.Y = Area.Margin;
                        }
                        cache.Save();
                }

                private void DownButton_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
                {
                        if (!cache.IsLogined)
                        {
                                MessageBox.Show("请登录后执行此操作");
                                return;
                        }

                        cache.CurrentArea.Y += cache.CurrentStep;
                        if (cache.CurrentArea.Y > Global.ImageCaptureHeight - Area.Margin)
                        {
                                cache.CurrentArea.Y = Global.ImageCaptureHeight - Area.Margin;
                        }
                        cache.Save();
                }

                private void RaiseButton_Click(object sender, System.Windows.RoutedEventArgs e)
                {
                        if (!cache.IsLogined)
                        {
                                MessageBox.Show("请登录后执行此操作");
                                return;
                        }

                        new RaiseOperation(cache.CurrentArea.ChannelId, true).Run();
                }

                private void FallButton_Click(object sender, System.Windows.RoutedEventArgs e)
                {
                        if (!cache.IsLogined)
                        {
                                MessageBox.Show("请登录后执行此操作");
                                return;
                        }
                        new RaiseOperation(cache.CurrentArea.ChannelId, false).Run();
                }

                private void ShiftButton_Click(object sender, System.Windows.RoutedEventArgs e)
                {
                        if (!cache.IsLogined)
                        {
                                MessageBox.Show("请登录后执行此操作");
                                return;
                        }

                        cache.Channels[cache.CurrentArea.ChannelId].ShiftState = ShiftState.待翻;
                        cache.ShiftOperations.Add(new ShiftOperation(cache.CurrentArea.ChannelId, true));
                }

                private void ResetButton_Click(object sender, System.Windows.RoutedEventArgs e)
                {
                        if (!cache.IsLogined)
                        {
                                MessageBox.Show("请登录后执行此操作");
                                return;
                        }

                        cache.Channels[cache.CurrentArea.ChannelId].ShiftState = ShiftState.未翻;
                        cache.ShiftOperations.Add(new ShiftOperation(cache.CurrentArea.ChannelId, false));
                }
        }
}